package com.cf.service.impl;

import com.cf.dao.ItemDao;
import com.cf.entity.Advert;
import com.cf.dao.AdvertDao;
import com.cf.entity.Item;
import com.cf.service.AdvertService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdvertServiceImpl implements AdvertService {
    @Autowired
    private AdvertDao advertDao;
    @Autowired
    private ItemDao itemDao;
    //查询所有
    @Override
    public PageInfo<Advert> selectAdvert(String iname,int pagenum, int pagesize) {
        PageHelper.startPage(pagenum, pagesize);
        Map map = new HashMap<>();
        map.put("iname",iname);
        List<Advert> adverts = advertDao.selectAdvert(map);
        PageInfo<Advert> pageInfo = new PageInfo<>(adverts);
        return pageInfo;
    }

    @Override
    public int deletetAdvert(long adid) {
        return advertDao.deletetAdvert(adid);
    }
    @Override
    //selectAdvertIsdel
    public PageInfo<Advert> selectAdvertIsdel(String iname,int pagenum, int pagesize) {
        PageHelper.startPage(pagenum, pagesize);
        Map map = new HashMap<>();
        map.put("iname",iname);
        List<Advert> adverts = advertDao.selectAdvertIsdel(map);
        PageInfo<Advert> pageInfo = new PageInfo<>(adverts);
        return pageInfo;
    }

    @Override
    public int deletetAdvertIsdel(Date starttime, Date endtime, long adid) {
        int i = advertDao.deletetAdvertIsdel(starttime, endtime, adid);
        return i;
    }

    //添加
    @Override
    public int insertAdvert(Advert advert) {
        return advertDao.insertAdvert(advert);
    }

    //真逻辑查询
    @Override
    public PageInfo<Advert> selectByStatus(int pagenum ,int pagesize) {
        PageHelper.startPage(pagenum, pagesize);
        List<Advert> adverts = advertDao.selectByStatus();
        if (adverts.size()!=0){
            for (Advert advert : adverts){
                Item item = itemDao.selectItemByIid(advert.getIid());
                advert.setIname(item.getIname());
                if (advert.getStatus()==0){
                    advert.setStatushz("未审核");
                }else {
                    advert.setStatushz("审核通过");
                }
            }
            PageInfo<Advert> pageInfo = new PageInfo<>(adverts);
            return pageInfo;
        }else {
            return null;
        }
    }

    @Override
    public List<Item> selectItemByAdvert() {
        List<Item> items = advertDao.selectItemByAdvert();
        for (Item item: items) {
            String s = "http://localhost:8888/crowdfunding_system_war_exploded/showItemImg?filename="+item.getIimg();
            item.setLink(s);
            double  percentage =  item.getNmoney()/item.getAmoney()*100;
            long round = Math.round(percentage);
            item.setPercentage(round);
        }
        return items;
    }
}
